<template>
  <div class="page fxActivitySigning">
    <div class="scroll-view">
      <customHeader middleTitle="商户签约验证" @leftCallBack="goBack"></customHeader>
      <div class="page-body">
        <div class="form-title">由于您的门店享受优惠费率，需要完成全部签约。</div>
        <ul class="form-style">
          <li v-for="(item,index) in list" :key="index">
            <span>{{payTypeFormate(item.payType)}}</span>
            <div
              @click="toSign(item,(item.allowStatus==1||item.allowStatus==2))"
              :class="{disabled:item.allowStatus==1||item.allowStatus==2}"
            >
              {{opeFormate(item.allowStatus)}}
              <i class="zhk-list_rarrow"></i>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import customHeader from "../../components/header/header";
import { Toast } from "mint-ui";
export default {
  name: "fxActivitySigning",
  data() {
    //allowStatus分账功能状态：0 未生效 1 审核中 2 开通 3 关闭 4 驳回
    return {
      queryParams: null,
      list: [],
      loadding: false,
    };
  },

  methods: {
    goBack() {
      const canSignCount = this.list.length-this.list.filter((item) => { return item.allowStatus == 1 || item.allowStatus == 2 }).length;//可签约的条数
      const backStep = canSignCount>0 ? -1 : -2;//返回步数
      this.$router.go(backStep);
    },
    payTypeFormate(payType) {
      let v = "";
      if (payType == "01") {
        v = "支付宝";
      } else if (payType == "02") {
        v = "微信";
      } else if (payType == "03") {
        v = "云闪付";
      } else if (payType == "00") {
        v = "微信+支付宝";
      }
      return v;
    },
    opeFormate(allowStatus) {
      let v = "去签约";
      if (allowStatus == 0) {
        v = "去签约";
      } else if (allowStatus == 1) {
        v = "审核中";
      } else if (allowStatus == 2) {
        v = "已签约";
      }
      return v;
    },
    loadData() {
      this.ajax({
        url: `/newapp/AppMsg/queryShopAllowParm.do?shopId=${this.queryParams.shopId}&msgId=${this.queryParams.msgId}`,
        type: "get",
        controlLoading: true
      }).then(res => {
        if (res.success && res.resultObject) {
          this.list = res.resultObject;
        }
      })
        .catch(err => {

        });
    },
    toSign(item, disabled) {
      if (disabled) {
        return;
      }
      if (item.merchantType == 8)//乐刷通道
      {
        this.hasProtocol(item);
        return;
      }
      if (item.accountType == "01") {//01对私账户02对公账户
        this.$router.push({
          path: "/todoList/fxActivitySigningDetails/fxActivitySigning/mobileValidate",
          query: { ...item, ...this.queryParams }
        });
      } else if (item.accountType == "02") {
        this.$router.push({
          path: "/todoList/fxActivitySigningDetails/fxActivitySigning/moneyValidate",
          query: { ...item, ...this.queryParams }
        });
      } else {
        Toast("账户类型错误");
      }
    },
    linkToIframe(signUrl) {
      this.$router.push({ path: '/iframe', query: { title: '在线电子签约', url: signUrl } });
    },
    //是否已经生成协议(乐刷)
    hasProtocol(item) {
      this.ajax({
        url: `/newapp/AppMsg/allowcateAgreementCheck.do?merchantId=${item.merchantId}`,
        type: "get",
        controlLoading: true
      })
        .then(result => {
          if (result.success.toString() == "false") {//没有生成协议
            //调用生成协议的接口去生成
            this.generateProtocol(item);
          } else {
            //生成过协议
            if (result.resultObject && result.resultObject['signUrl']) {//跳转到乐刷页面
              this.linkToIframe(result.resultObject['signUrl']);
            } else {
              Toast("未获取到已有协议");
            }
          }
        })
        .catch(err => {

        });
    },

    //生成协议并且修改消息状态(乐刷)
    generateProtocol(item) {
      this.ajax({
        url: `/newapp/AppMsg/getMarketingOauthCode.do?payType=${item.payType}&marketingId=${this.queryParams.openId}&shopId=${this.queryParams.shopId}&msgId=${this.queryParams.msgId}&phoneNumber=""`,
        type: "get",
        controlLoading: true
      })
        .then(res => {
          if (res.success.toString() == "true") {
            //this.updateMsgState();
            this.linkToIframe(res.resultObject['signUrl']);
          } else {
            Toast(res.msg);
          }
        })
        .catch(err => {

        });
    },
    //修改消息状态
    updateMsgState() {
      if (this.loadding) {
        return;
      }
      this.loadding = true;
      let param = {
        msgId: this.queryParams.msgId,
        msgStatus: 1,
        msgTitleType: this.queryParams.msgTitleType,
        openId: this.queryParams.openId,
        shopId: this.queryParams.shopId,
        needOpenMarketing: this.queryParams.needOpenMarketing
      };
      this.ajax({
        url: "/newapp/AppMsg/updateMsgState.do",
        type: "post",
        data: param,
        controlLoading: false
      }).then(res => {
        this.loadding = false;
        if (res.success) {
        } else {
          Toast(res.msg);
        }
      })
        .catch(err => {
          this.loadding = false;
        });
    }


  },

  created() {
    this.queryParams = this.$route.query;
    this.loadData();
  },
  components: {
    customHeader,
  },

};
</script>
<style lang='scss' scoped>
@import "fxActivitySigning";
</style>
